var pageindex = 1; //当前页数
var pages = 1; //总页数
var load_length = 1600; //指定距离加载一次

$(function() {
	get_user_mess_and_token();
	if(user_id==0){
		location.href="http://127.0.0.1:8899/html/car_login.html?car_my_follow";
		return;
	}
	//点击关注用户和关注车系，查看所有
	getClick();
	//根据登入id查询登录用户的信息
	getByIdUser();
	//关注数
	getGuanZhu();
	//粉丝数
	getFenSi();
	//登录
	$(".to_login").attr("href",
		"http://127.0.0.1:8899/html/car_login.html?car_my_follow/" + user_id);
});

/**
 * 点击关注用户和关注车系，查看所有
 */
function getClick() {
	//查询所有被关注的用户
	getFollow_User();
	//判断是否关注用户
	existsFollowUser();
	$("#LoveUser").click(function() {
		$("#LoveUser span").addClass("jsx-694917773 active tw-mr-12 tab-button");
		$("#LoveCarSeries span").removeClass("jsx-694917773 active tw-mr-12 tab-button");
		$("#LoveCarSeries span").addClass("jsx-694917773 tab-button");
		getFollow_User();
		existsFollowUser();
		$("#Follow_Car_Series_list a").remove();
	});
	$("#LoveCarSeries").click(function() {
		$("#LoveCarSeries span").addClass("jsx-694917773 active tw-mr-12 tab-button");
		$("#LoveUser span").removeClass("jsx-694917773 active tw-mr-12 tab-button");
		$("#LoveUser span").addClass("jsx-694917773 tab-button");
		getFollow_Car_Series();
		existsFollowCarSeries();
		$("#Follow_User_list div").remove();
	});
}

/**
 * 判断是否关注用户
 */
function existsFollowUser() {
	$.post("http://127.0.0.1:8899/api/consumer/Follow_userController/select_Follow_user/" + user_id,
		function(data) {
			if (data != null && data != '') {
				$(".No_follow_user").hide();
			} else {
				$(".No_follow_user").show();
				$("#CarSeriesSpan").hide();
				$("#NoMores").hide();
				$("#UserSpan").show();
			}
		}, "JSON")
}

/**
 * 判断是否关注车系
 */
function existsFollowCarSeries() {
	$.post("http://127.0.0.1:8899/api/consumer/Follow_car_seriesController/select_follow_car_series/" +
		user_id,
		function(data) {
			if (data != null && data != '') {
				$(".No_follow_user").hide();
			} else {
				$(".No_follow_user").show();
				$("#UserSpan").hide();
				$("#NoMores").hide();
				$("#CarSeriesSpan").show();
			}
		}, "JSON")
}


/**
 * 查询所有被关注的用户
 */
function getFollow_User() {
	$.post("http://127.0.0.1:8899/api/consumer/Follow_userController/select_Follow_user/" + user_id,
		function(data) {
			var str = "";
			$(data).each(function(index, item) {
				str += "<div>" +
					"<section class='jsx-676849290 user-list-card tw-flex tw-cursor-pointer'>" +
					"<div class='jsx-676849290 user-item'>" +
					"<div class='jsx-676849290 left'>" +
					"<div class='jsx-676849290 author-avatar-wrap'>" +
					"<div class='jsx-2217341492 avatar_avatar-img__170Bh tw-text-0'style='margin-right: 8px;'>" +
					"<a href='http://127.0.0.1:8899/html/car_my_homepage.html?" + item.user.user_id +
					"'title='" + item.user.user_name + "个人主页 'target='_blank'rel='opener'>" +
					"<div class='avatar tw-rounded-full'style='display: inline-block; max-width: 100%; overflow: hidden; position: relative; z-index: 0;'>" +
					"<div style='box-sizing: border-box; display: block; max-width: 100%;'>" +
					"<img alt=''src='data:image/svg+xml;utf-8,<svg width=&quot;80&quot; height=&quot;80&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>'aria-hidden='true'role='presentation'style='max-width: 100%; display: block; margin: 0px; border: none; padding: 0px;'>" +
					"</div>" +
					"<img src='http://120.48.47.144:8899/images/" + item.user.user_head_portrait_url +
					"' alt='" + item.user.user_name +
					"头像'width='80'height='80'class=''style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: cover;'>" +
					"</div>" +
					"</a>" +
					"</div>" +
					"</div>" +
					"<div style='position: relative;left:95px;top: -80px;'>" +
					"<div class='jsx-676849290 header'>" +
					"<h3 class='jsx-676849290 name line-1'>" + item.user.user_name + "</h3>" +
					"</div>" +
					"<div class='jsx-676849290 numbers tw-my-8 tw-leading-20'>" +
					"<span class='jsx-676849290 focus'><span class='jsx-676849290 number' style='color:red'>" +
					item.followcount + "</span>关注</span>&nbsp;&nbsp;" +
					"<span class='jsx-676849290 fans'><span class='jsx-676849290 number' style='color:red'>" +
					item.covercount + "</span>粉丝</span>" +
					"</div>" +
					"<p class='jsx-676849290 desc line-2'>" + item.user.user_personal_signature + "</p>" +
					"</div>" +
					"</div>" +
					"<div style='position: relative;left:525px;top: -119px;'>" +
					"<span onclick=\"insert_or_delete_follow_user_id('delete',this," + item
					.follow_cover_user_fk_id +
					",)\" class='follow tw-w-76 xl:tw-w-96 2xl:tw-w-111 button new_primary button_root__3V6Iu button_new_primary__2mZuy'role='button'style='line-height: 36px; display: table-cell;'>已关注</span>" +
					"<span onclick=\"insert_or_delete_follow_user_id('insert',this," + item
					.follow_cover_user_fk_id +
					")\" class='follow tw-w-76 xl:tw-w-96 2xl:tw-w-111 button new_primary button_root__3V6Iu button_new_primary__2mZuy'role='button'style='line-height: 36px;  display: none; '>关注</span>" +
					"</div>" +
					"</div>" +
					"</section>" +
					"</div>";
			})
			$("#Follow_Model div").remove();
			$("#Follow_Model a").remove();
			$("#Follow_Model").append(str);
		}, "JSON");
}

/**
 * 关注和取消关注和的点击事件
 */
function insert_or_delete_follow_user_id(types, element, cover_user_id) {
	$.ajax({
		url: "http://127.0.0.1:8899/api/consumer/Follow_userController/insert_or_delete_follow_user_id",
		type: "post",
		data: JSON.stringify({
			"follow_user": {
				"follow_user_fk_id": user_id,
				"follow_cover_user_fk_id": cover_user_id
			},
			"type": types
		}),
		contentType: 'application/json;charset=utf-8',
		dataType: "json",
		success: function(data) {
			if (data.result == "true") {
				if (types == "insert") {
					$(element).hide();
					$(element).prev().show();
				} else if (types == "delete") {
					$(element).hide();
					$(element).siblings("span").show();
				}
			}
		}
	});
}

/**
 * 查询被关注的车系
 */
function getFollow_Car_Series() {
	$.post("http://127.0.0.1:8899/api/consumer/Follow_car_seriesController/select_follow_car_series/" +
		user_id,
		function(data) {
			var str = "";
			$(data).each(function(index, item) {
				str +=
					"<a target='_blank' class='jsx-1919295653 focus-series-card tw-h-108' href='http://127.0.0.1:8899/html/car_series_index.html?" +
					item.car_series.car_series_id + "' style='display: flex;'>" +
					"<div class='jsx-1919295653 series-item'>" +
					"<div class='jsx-1919295653 left'>" +
					"<div class='jsx-1919295653 cover-img-wrap'>" +
					"<div style='display: block; overflow: hidden; position: relative; z-index: 0;'>" +
					"<div style='display: block; box-sizing: border-box; padding-top: 56.129%;'></div>" +
					"<img src='http://120.48.47.144:8899/images/" + item.car_series
					.car_series_images_url_1 + "' alt='" + item.car_series.car_series_name +
					"' width='155' height='87' class='zoom' style='position: absolute; inset: 0px; box-sizing: border-box; padding: 0px; border: none; margin: auto; display: block; width: 0px; height: 0px; min-width: 100%; max-width: 100%; min-height: 100%; max-height: 100%; object-fit: contain;'>" +
					"</div>" +
					"<button type='button' class='focus-btn tw-hidden tw-absolute tw-z-10 tw-right-0 tw-top-0 tw-h-24 tw-px-4 tw-rounded-bl-2 tw-bg-common-black tw-text-white tw-bg-opacity-50 hover:tw-bg-opacity-80'>关注</button>" +
					"</div>" +
					"<div class='jsx-1919295653 series-info ' style='position: relative;left: 150px;top: -60px;'>" +
					"<h3 class='jsx-1919295653 name line-1'>" + item.car_series.car_series_name + "</h3>" +
					"<p class='jsx-1919295653 price'>指导价:<span class='jsx-1919295653 values' style='color:red'>" +
					item.car_series.min_manufacturer_price + '-' + item.car_series.max_manufacturer_price +
					"万</span></p>" +
					"</div>" +
					"</div>" +
					"<div class='jsx-1919295653 right' style='position: relative;left: 520px;top: -97px;'>" +
					"<span onclick=\"JumpBuyCarCalculate(" + item.car_series.car_series_id +
					")\" class='follow  tw-w-76 xl:tw-w-96 2xl:tw-w-111 button new_primary button_root__3V6Iu button_new_primary__2mZuy' role='button'>购车计算</span>" +
					"</div>" +
					"</div>" +
					"</a>";
			})
			$("#Follow_Model a").remove();
			$("#Follow_Model div").remove();
			$("#Follow_Model").append(str);
		}, "JSON");
}

/**
 * 跳转至购车计算页面
 */
function JumpBuyCarCalculate(series_ids) {
	location.href = "http://127.0.0.1:8899/html/car_purchase_calculation.html?" + series_ids + "/0";
}

/**
 * 根据登入id查询登录用户的信息
 */
function getByIdUser() {
	$.post("http://127.0.0.1:8899/api/consumer/Follow_userController/select_ById/" + user_id, function(
		data) {
		str = "<span><div class='jsx-2665565346 author-avatar-wrap'>" +
			"<div class='jsx-2217341492 avatar_avatar-img__170Bh tw-text-0'>" +
			"<div style='display:inline-block;max-width:100%;overflow:hidden;position:relative;z-index:0' class='avatar tw-rounded-full'>" +
			"<div style='box-sizing:border-box;display:block;max-width:100%'>" +
			"<img style='max-width:100%;display:block;margin:0;border:none;padding:0' alt='' src='data:image/svg+xml;utf-8,&lt;svg width=&quot;100&quot; height=&quot;100&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/&gt;' aria-hidden='true' role='presentation' />" +
			"</div>" +
			"<a href='http://127.0.0.1:8899/html/car_my_homepage.html?" + data.user.user_id +"'>"+
			"<noscript>" +
			"<img src='http://120.48.47.144:8899/images/" + data.user.user_head_portrait_url + "' alt='" + data
			.user.user_name +
			"头像' width='100' height='100' style='position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover' class='' />" +
			"</noscript>" +
			"<img src='http://120.48.47.144:8899/images/" + data.user.user_head_portrait_url + "' alt='" + data
			.user.user_name +
			"头像' width='100' height='100' style='position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:cover' class='' /></a>" +
			"</div>" +
			"</div>" +
			"</div>" +
			"<a href='http://127.0.0.1:8899/html/car_my_homepage.html?" + data.user.user_id +"'>"+
			"<div class='jsx-2665565346 author-basic-info'>" +
			"<h1 title='" + data.user.user_name +
			"' class='jsx-2665565346 tw-text-24 tw-font-medium tw-w-285 lg:tw-w-390 line-1'>" + data.user
			.user_name + "</h1>" +
			"<p title='" + data.user.user_personal_signature +
			"' class='jsx-2665565346 tw-mt-12 tw-text-common-gray tw-w-285 lg:tw-w-390 line-1'>" +
			"<span class='jsx-2665565346'>简介：</span>" + data.user.user_personal_signature + "</p>" +
			"</div></a></span>";
		$("#byIdUser span").remove();
		$("#byIdUser").append(str);
	}, "JSON")
}

/**
 * 查询关注数
 */
function getGuanZhu() {
	$.post("http://127.0.0.1:8899/api/consumer/Follow_userController/select_ById/" + user_id, function(
		data) {
		str =
			"<a class='jsx-2665565346 counter active tw-mr-24 md:tw-mr-40 xl:tw-mr-60' href='javascript:void(0)'>" +
			"<span class='jsx-2665565346 tw-text-16 md:tw-text-18'>" + data.followcount + "</span>" +
			"<span class='jsx-2665565346 tw-text-12 md:tw-text-14'>关注</span></a>";
		$("#guanzhu a").remove();
		$("#guanzhu").append(str);
	}, "JSON");
}

/**
 * 查询粉丝数
 */
function getFenSi() {
	$.post("http://127.0.0.1:8899/api/consumer/Follow_userController/select_By_CoverId/" + user_id,
		function(data) {
			str = "<a class='jsx-2665565346 counter undefined tw-mr-24 md:tw-mr-40 xl:tw-mr-60' href='javascript:void(0)'>" +
				"<span class='jsx-2665565346 tw-text-16 md:tw-text-18'>" + data.covercount + "</span>" +
				"<span class='jsx-2665565346 tw-text-12 md:tw-text-14'>粉丝</span></a>";
			$("#fensi a").remove();
			$("#fensi").append(str);
		}, "JSON");
}
